 package provider;

 import com.fasterxml.jackson.core.type.TypeReference;
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;

 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 import java.util.HashMap;
 import java.util.Map;
 import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.KeyGenerator;
 import javax.crypto.spec.SecretKeySpec;
 import tools.JsonUtil;

 public class MapleOverrideData {
/*  22 */   private static MapleOverrideData instance = new MapleOverrideData();
/*  23 */   public String key = "4652352523425";
   private KeyGenerator kgen;
   private Cipher cipher;
   private SecretKeySpec secretKeySpec;
   private Map<Integer, Map<String, String>> overridedata;

   MapleOverrideData() {
     try {
/*  31 */       this.kgen = KeyGenerator.getInstance("AES");
/*  32 */       this.kgen.init(128, new SecureRandom(this.key.getBytes()));
/*  33 */       this.cipher = Cipher.getInstance("AES");
/*  34 */       this.secretKeySpec = new SecretKeySpec(this.kgen.generateKey().getEncoded(), "AES");
/*  35 */     } catch (NoSuchAlgorithmException|javax.crypto.NoSuchPaddingException e) {
/*  36 */       e.printStackTrace();
     }
   }

   public static MapleOverrideData getInstance() {
/*  41 */     return instance;
   }







   public static String parseByte2HexStr(byte[] buf) {
/*  51 */     StringBuffer sb = new StringBuffer();
/*  52 */     for (int i = 0; i < buf.length; i++) {
/*  53 */       String hex = Integer.toHexString(buf[i] & 0xFF);
/*  54 */       if (hex.length() == 1) {
/*  55 */         hex = '0' + hex;
       }
/*  57 */       sb.append(hex.toUpperCase());
     }
/*  59 */     return sb.toString();
   }







   public static byte[] parseHexStr2Byte(String hexStr) {
/*  69 */     if (hexStr.length() < 1)
/*  70 */       return null; 
/*  71 */     byte[] result = new byte[hexStr.length() / 2];
/*  72 */     for (int i = 0; i < hexStr.length() / 2; i++) {
/*  73 */       int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
/*  74 */       int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
/*  75 */       result[i] = (byte)(high * 16 + low);
     }
/*  77 */     return result;
   }







   public String encrypt(byte[] json) {
     try {
/*  88 */       this.cipher.init(1, this.secretKeySpec);
/*  89 */       return parseByte2HexStr(this.cipher.doFinal(json));
/*  90 */     } catch (IllegalBlockSizeException|javax.crypto.BadPaddingException|InvalidKeyException e) {
/*  91 */       e.printStackTrace();

/*  93 */       return null;
     }
   }






   public String decrypt(byte[] data) {
     try {
/* 104 */       this.cipher.init(2, this.secretKeySpec);
/* 105 */       return new String(this.cipher.doFinal(data));
/* 106 */     } catch (InvalidKeyException|javax.crypto.BadPaddingException|IllegalBlockSizeException e) {
/* 107 */       e.printStackTrace();

/* 109 */       return null;
     }
   }



   public void init() {
/* 116 */     File file = new File("config\\overridedata.dat");
/* 117 */     if (!file.exists()) {
       try {
/* 119 */         if (!file.createNewFile()) {
/* 120 */           System.out.println("找不到 overridedata.json 文件，读取失败。");
           return;
         }
/* 123 */       } catch (IOException e) {
/* 124 */         e.printStackTrace();
       }
     }
     try {
/* 128 */       if (file.length() != 0L) {
/* 129 */         try (FileInputStream inputStream = new FileInputStream(file)) {
/* 130 */           byte[] buffer = new byte[inputStream.available()];
/* 131 */           inputStream.read(buffer);
/* 132 */           this.overridedata = (Map<Integer, Map<String, String>>)JsonUtil.getMapperInstance().readValue(decrypt(parseHexStr2Byte(new String(buffer, "UTF-8"))), new TypeReference<Map<Integer, Map<String, String>>>() {  }
             );
         }
       } else {
/* 136 */         this.overridedata = new HashMap<>();
       }
/* 138 */     } catch (IOException e) {
/* 139 */       e.printStackTrace();
     }
   }




   public void save() {
/* 147 */     File file = new File("config\\overridedata.dat");
/* 148 */     if (!file.exists()) {
       try {
/* 150 */         if (!file.createNewFile()) {
/* 151 */           System.out.println("找不到 overridedata.json 文件，保存失败。");
           return;
         }
/* 154 */       } catch (IOException e) {
/* 155 */         e.printStackTrace();
       }
     }

/* 159 */     try(FileOutputStream outputStream = new FileOutputStream(file); 
/* 160 */         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
/* 161 */       bufferedOutputStream.write(encrypt(JsonUtil.getMapperInstance().writeValueAsBytes(this.overridedata)).getBytes());

     }
/* 164 */     catch (IOException e) {
/* 165 */       e.printStackTrace();
     }
   }

   public Map<Integer, Map<String, String>> getOverridedata() {
/* 170 */     return this.overridedata;
   }

   public String getOverrideValue(int skillid, String name) {
/* 174 */     if (this.overridedata.containsKey(Integer.valueOf(skillid)) && ((Map)this.overridedata.get(Integer.valueOf(skillid))).containsKey(name)) {
/* 175 */       return (String)((Map)this.overridedata.get(Integer.valueOf(skillid))).get(name);
     }
/* 177 */     return "";
   }
 }


/* Location:              C:\PGX\Server.jar!\provider\MapleOverrideData.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */